package com.cocolove2.library_cocodialog.dialogs.base;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cocolove2.library_cocodialog.entity.DialogMenuItem;
import com.cocolove2.library_cocodialog.listener.AnimListenerAdapter;

import java.util.ArrayList;

@SuppressWarnings({"unused", "unchecked"})
public abstract class CocoListDialogBase<T extends CocoDialogBase> extends CocoDialogBase<T> {
    /*ListView*/
    protected ListView mLv;
    /*title*/
    protected TextView mTvTitle;
    /* title background color(标题背景颜色)*/
    protected int mTitleBgColor = Color.parseColor("#303030");
    /* title text(标题)*/
    protected String mTitle;
    /* title textcolor(标题颜色)*/
    protected int mTitleTextColor = Color.parseColor("#ffffff");
    /* title textsize(标题字体大小,单位sp)*/
    protected float mTitleTextSize = 16.5f;
    /* divider color(ListView divider颜色)*/
    protected int mDividerColor = Color.LTGRAY;
    /* divider height(ListView divider高度)*/
    protected float mDividerHeight = 0.8f;
    /* item press color(ListView item按住颜色)*/
    protected int mItemPressColor = Color.parseColor("#ffcccccc");
    /* item textcolor(ListView item文字颜色)*/
    protected int mItemTextColor = Color.parseColor("#303030");
    /* item textsize(ListView item文字大小)*/
    protected float mItemTextSize = 15f;
    /* item extra padding(ListView item额外padding)*/
    protected int mItemExtraLeft;
    protected int mItemExtraTop;
    protected int mItemExtraRight;
    protected int mItemExtraBottom;
    /* adapter(自定义适配器)*/
    protected BaseAdapter mAdapter;
    /* operation items(操作items)*/
    protected ArrayList<DialogMenuItem> mContents = new ArrayList<>();

    private OnClickListener mOnClickListener;

    private int mSelectPosition = -1;
    /*listview item 的动画*/
    protected LayoutAnimationController mLac;

    protected final AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mSelectPosition = position;
            if (!isHasExitAnim()) {//无退出动画
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(CocoListDialogBase.this, mSelectPosition);
                }
                CocoListDialogBase.this.realDismiss();
            } else {//有退出动画
                CocoListDialogBase.this.dismiss();
            }
        }
    };


    public CocoListDialogBase(Context context) {
        super(context);
    }

    public CocoListDialogBase(Context context, int style) {
        super(context, style);
    }


    @Override
    protected void setupUiView() {
        setOutAnimListener(new AnimListenerAdapter() {
            @Override
            public void onAnimEnd() {
                if (mOnClickListener != null && mSelectPosition > -1)
                    mOnClickListener.onClick(CocoListDialogBase.this, mSelectPosition);
            }
        });
    }

    /**
     * 设置数据源和点击事件
     *
     * @param items    数据源
     * @param listener 点击时间
     */
    public T setItems(@NonNull ArrayList<DialogMenuItem> items, OnClickListener listener) {
        mContents.clear();
        mContents.addAll(items);
        mOnClickListener = listener;
        return (T) this;
    }

    /**
     * 设置数据源
     *
     * @param items    数据源
     * @param adapter  数据适配器
     * @param listener 点击时间
     */
    public T setItems(@NonNull ArrayList<DialogMenuItem> items, BaseAdapter adapter, OnClickListener listener) {
        mContents.clear();
        mContents.addAll(items);
        mAdapter = adapter;
        mOnClickListener = listener;
        return (T) this;
    }

    /**
     * 设置数据源
     *
     * @param items    数据源
     * @param listener 点击时间
     */
    public T setItems(@NonNull String[] items, OnClickListener listener) {
        mContents.clear();
        for (String item : items) {
            DialogMenuItem customBaseItem = new DialogMenuItem(item, 0);
            mContents.add(customBaseItem);
        }
        mOnClickListener = listener;
        return (T) this;
    }


    /**
     * set title background color(设置标题栏背景色) @return NormalListCocoDialog
     */
    public T setTitleBgColor(int titleBgColor) {
        mTitleBgColor = titleBgColor;
        return (T) this;
    }

    /**
     * set title text(设置标题内容)
     */
    public T setTitle(String title) {
        mTitle = title;
        return (T) this;
    }

    /**
     * set title textsize(设置标题字体大小)
     */
    public T setTitleTextSize(float titleTextSize_SP) {
        mTitleTextSize = titleTextSize_SP;
        return (T) this;
    }

    /**
     * set title textcolor(设置标题字体颜色)
     */
    public T setTitleTextColor(int titleTextColor) {
        mTitleTextColor = titleTextColor;
        return (T) this;
    }


    /**
     * set divider color(ListView divider颜色)
     */
    public T setDividerColor(int dividerColor) {
        mDividerColor = dividerColor;
        return (T) this;
    }

    /**
     * set divider height(ListView divider高度)
     */
    public T setDividerHeight(float dividerHeight_DP) {
        mDividerHeight = dividerHeight_DP;
        return (T) this;
    }

    /**
     * set item press color(item按住颜色)
     */
    public T setItemPressColor(int itemPressColor) {
        mItemPressColor = itemPressColor;
        return (T) this;
    }

    /**
     * set item textcolor(item字体颜色)
     */
    public T setItemTextColor(int itemTextColor) {
        mItemTextColor = itemTextColor;
        return (T) this;
    }

    /**
     * set item textsize(item字体大小)
     */
    public T setItemTextSize(float itemTextSize_SP) {
        mItemTextSize = itemTextSize_SP;
        return (T) this;
    }

    /**
     * set item height(item高度)
     */
    public T setItemExtraPadding(int itemLeft, int itemTop, int itemRight, int itemBottom) {
        mItemExtraLeft = dp2px(itemLeft);
        mItemExtraTop = dp2px(itemTop);
        mItemExtraRight = dp2px(itemRight);
        mItemExtraBottom = dp2px(itemBottom);

        return (T) this;
    }

    /**
     * set layoutAnimation(设置layout动画 ,传入null将不显示layout动画)
     */
    public T setLayoutAnimation(LayoutAnimationController lac) {
        mLac = lac;
        return (T) this;
    }

    protected abstract View getAdapterView(int position, View convertView, ViewGroup parent);

    public final class ListDialogAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mContents.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressWarnings("deprecation")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getAdapterView(position, convertView, parent);
        }
    }
}
